import request from './auth'

/**
 * 获取拍卖列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export function getAuctionList(params) {
    return request({
        url: '/admin/auction/page',
        method: 'get',
        params
    })
}

/**
 * 获取拍卖详情
 * @param {Number} id - 拍卖ID
 * @returns {Promise}
 */
export function getAuctionDetailAdmin(id) {
    return request({
        url: `/admin/auction/${id}`,
        method: 'get'
    }).then(res => {
        console.log('获取拍卖详情响应:', res)
        return res.data === undefined ? res : res.data
    })
}

/**
 * 审核拍卖品
 * @param {Number} id - 拍卖ID
 * @param {String} status - 审核状态
 * @returns {Promise}
 */
export function reviewAuction(id, status) {
    return request.put(`/admin/auction/${id}/review`, null, { params: { status } })
}

/**
 * 获取拍卖详情（用户端）
 * @param {Number} id - 拍卖ID
 * @returns {Promise}
 */
export function getAuctionDetail(id) {
    return request({
        url: `/user/auction/${id}`,
        method: 'get'
    }).then(res => {
        console.log('获取拍卖详情响应:', res)
        return res.data === undefined ? res : res.data
    })
}

/**
 * 根据分类获取拍卖列表
 * @param {Number|String} categoryId - 分类ID
 * @param {Object} params - 其他查询参数
 * @returns {Promise}
 */
export function getAuctionsByCategory(categoryId, params = {}) {
    const requestParams = { ...params };

    // 只有当categoryId有值时才添加到请求参数中
    if (categoryId !== undefined && categoryId !== null && categoryId !== '') {
        requestParams.categoryId = Number(categoryId);
    }

    return request({
        url: '/user/auction/list',
        method: 'get',
        params: requestParams
    }).then(res => {
        console.log('根据分类获取拍卖列表响应:', res)
        return res.data === undefined ? res : res.data
    })
}